<script>
  function addFavourite(){
    var uid = "<!--[$user.uid]-->";
    if(uid){
      //var favourite = document.getElementById('fav_value').value;
    var forum_id = <!--[$smarty.get.id]-->;
    var topic_uid = <!--[$extendResult.topics.cr_uid]-->;
    var url = "index.php?module=InnoForum&type=ajax&func=addFavourite&ctrl=Favourite"
              +"&uid="+uid
              +"&topic_id="+forum_id
              +"&topic_uid="+topic_uid
              +"&s="+ genSession() ;
    //alert(url);
    //Call ajax
      var xmlhttp;
      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
      if(xmlhttp.readyState==4) {
        //document.myForm.time.value=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);

      alert("เพิ่มในรายการโปรดเรียบร้อยแล้ว")
    } else{
      alert("Error : กรุณาทำการล๊อกอินก่อน");
    }
  }

  function sendEmail(obj){
    //Check mail to
    var sendto = document.getElementById('sendto');
    var sendto_val = sendto.value;
    if (trimString(sendto_val)=='') {
      alert("กรุณากรอก email ที่คุณต้องการส่งถึง");
      sendto.focus();
      return false;
    }
    if (!isEmail(sendto_val)) {
      var cont = confirm("Your email address does not seem to be in a correct format.  Press OK to confirm.  To make changes please press CANCEL");
      if (!cont) {
        sendto.focus();
        return false;
      }
    }

    //Check mail from
    var sendfrom = document.getElementById('sendfrom');
    var sendfrom_val = sendfrom.value;
    if (trimString(sendfrom_val)=='') {
      alert("กรุณากรอก email ของคุณ");
      sendfrom.focus();
      return false;
    }
    if (!isEmail(sendfrom_val)) {
      var cont = confirm("Your email address does not seem to be in a correct format.  Press OK to confirm.  To make changes please press CANCEL");
      if (!cont) {
        sendfrom.focus();
        return false;
      }
    }

    //Check mail to
    var captcha = document.getElementById('captcha');
    if (trimString(captcha.value)=='') {
      alert("กรุณาตอบคำถาม");
      captcha.focus();
      return false;
    }




    var message = document.getElementById('message').value;
    var forum_id = <!--[$smarty.get.id]-->;
    var sendmailform = document.getElementById('sendmail-form');
    
    var url = "index.php?module=InnoForum&type=ajax&func=sendEmail&sendto="+sendto_val
              +"&sendfrom="+sendfrom_val
              +"&message="+message
              +"&forum_id="+forum_id
              +"&captcha="+captcha.value
              +"&s="+ genSession() ;

    //Call ajax
      var xmlhttp;
      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

      xmlhttp.onreadystatechange=function() {
      document.getElementById('progress').innerHTML='<IMG SRC="modules/InnoForum/pnimages/ajax-loader-2.gif"> waiting...';
      if(xmlhttp.readyState==4) {
          var responseText = trimString(xmlhttp.responseText);

          if(responseText == "true"){
            document.getElementById('progress').innerHTML='';
            alert("ทำการส่ง email เรียบร้อยแล้ว");
            sendmailform.style.display = 'none';
          }else{
            document.getElementById('progress').innerHTML='';
            alert("คำตอบไม่ถูกต้อง");
            document.getElementById('captcha').value="";
            document.getElementById('captcha').focus();
          }

        }
      }
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
  }

  function trimString (str) 
  {
    str = this != window? this : str;
    return str.replace(/\s+/g, '').replace(/\s+$/g, '');
  }

  function isEmail(incoming) 
  {
    var emailstring = incoming;
    var ampIndex = emailstring.indexOf("@");
    var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
      // find a dot in the portion of the string after the ampersand only
    var dotIndex = afterAmp.indexOf(".");
      // determine dot position in entire string (not just after amp portion)
    dotIndex = dotIndex + ampIndex + 1;
      // afterAmp will be portion of string from ampersand to dot
    afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
      // afterDot will be portion of string from dot to end of string
    var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
    var beforeAmp = emailstring.substring(0,(ampIndex));
      //old regex did not allow subdomains and dots in names
      //var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
    var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
      // index of -1 means "not found"
    if ((emailstring.indexOf("@") != "-1") &&
      (emailstring.length > 5) &&
      (afterAmp.length > 0) &&
      (beforeAmp.length > 1) &&
      (afterDot.length > 1) &&
      (email_regex.test(emailstring)) ) {
        return true;
    } else {
        return false;
    }
  }

  function genSession(){
    return '&r=' + Math.random(999);
  }

</script>
<div class="view_header">
<a href="<!--[pnmodurl modname='InnoForum' func='form' ctrl='topics']-->">
<img src="<!--[modulepath]-->/pnimages/21.png" align="absmiddle"/>
<!--[pnml name='_INNOFORUM_USER_STD_HEADER_NewTopic']-->
</a>
<a href="<!--[pnmodurl modname='InnoForum' func='list' ctrl='favourite']-->">
<IMG SRC="modules/InnoForum/pnimages/fav_enable.gif" align="absmiddle" >      
<!--[pnml name='_INNOFORUM_USER_STD_HEADER_MyFavourite']-->
</a>
<!--[if intval($user.level) gte 300]-->
<a href="<!--[pnmodurl modname='InnoForum' func='list' ctrl='my_topics' user_id=$user.uid]-->">
<img src="<!--[modulepath]-->/pnimages/folder_page.png" align="absmiddle"/>
<!--[pnml name='_INNOFORUM_USER_STD_HEADER_MyTopic']-->
</a>
<!--[/if]-->
<INPUT TYPE="hidden" NAME="fav_value" VALUE="0" id="fav_value"><a HREF="#" onclick="addFavourite();"><img src="modules/InnoForum/pnimages/fav_disable.gif" align="absmiddle" ><!--[pnml name='_INNOFORUM_USER_STD_HEADER_ADDMyFavourite']--></a>





</div>     




